﻿#region Copyright Statement
//  ------------------------------------------------------------------------------
//  
//    This file is part of The Streambolics Library.
//    Copyright © 2005-2009, Stephan Leclercq & Streambolics
//    All rights reserved.
//  
//    The Streambolics Library is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//  
//    The Streambolics Library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//  
//    You should have received a copy of the GNU General Public License
//    along with Streambolics; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//  
//  ------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Streambolics.Hp41s
{
    /// <summary>
    ///     An execution context corresponding to a running program.
    /// </summary>
    /// <remarks><para>
    ///     The context does not implement a return stack. When encountering
    ///     a <c>XEQ</c> instruction, a new context is created and pushed on
    ///     the context stack. Therefore, HP-41S does not have the 6-level 
    ///     return stack limitation of an HP-41C.
    /// </para></remarks>

    public class RunningContext : Context
    {
        private bool _KeepRunning;
        private Steps _Program;
        private int _ProgramCounter;

        public RunningContext (Context aParent, Steps aProgram, int aProgramCounter)
            : base (aParent)
        {
            _Program = aProgram;
            _ProgramCounter = aProgramCounter;
        }

        protected internal override void ContextSwitch ()
        {
            _KeepRunning = false;
        }

        protected internal override bool Run ()
        {
            _KeepRunning = true;
            while (_KeepRunning)
            {
                Execute (_Program[_ProgramCounter++]);
            }
            return true;
        }

        public override string LongListing ()
        {
            return _Program.LongListing (_ProgramCounter);
        }

        public override void DoReturn ()
        {
            Machine.PopContext ();
        }

        public override void DoPrompt (string aDisplay)
        {
            new IdleContext (this, aDisplay);
        }
        protected internal override string GetDisplay ()
        {
            return Machine.GetBirds();
        }

        protected internal override void Execute (Instruction aInstruction)
        {
            aInstruction.ExecuteRunning (this);
        }
    }
}
